/* 
 * File:   Login.cpp
 * Author: frank
 *
 * Created on 2013年10月23日, 下午8:57
 */

#include <cstdio>
#include <conio.h>
#include"FileMng.h"


////////////////////////////////////////////////////////////////////////
//
//							登录部分
//
////////////////////////////////////////////////////////////////////////

//验证密码的函数 
//无此用户名返回1 密码错误返回2 成功登录返回3
int verify(char *name,char *psd)
{
	//将密码文件导入内存
	short psdMemo;
	int i;
	psdMemo=loadToMemo(SECT_PSDFILE);
	User *userPwd;
	userPwd=(User*)strMemo[psdMemo];
	
	for(i=0;i<USER_MAX;i++){
		if(strcmp(name,userPwd[i].name)==0){
			if(strcmp(psd,userPwd[i].psd)==0){
				unloadMemo(psdMemo);//将密码文件导出内存
				return 3;  //登录成功
			}
			unloadMemo(psdMemo);//将密码文件导出内存
			return 2;  //密码错误
		}
	}
	unloadMemo(psdMemo);//将密码文件导出内存
	return 1;
}


int haveLoged(char *name)
{
	int i,openNum,memoNum;
	char stream[CONTENT_MAX],temp[20];
	char *p,*q;

	openNum=FAIL;
	while(openNum==FAIL){
		openNum=loadFile("/online",RANDW,"online");
		if(openNum==FAIL){
			printf("System is busy... Please try again later.\n");
			unloadFile("/online","root");
		}
	}

	memoNum=openFileMemo[openNum].sectMemo[0][1];
	strcpy(stream,strMemo[memoNum]);
	p=stream;

	while(1){	
		q=p;
		if(*p=='\0')
			break;
		for(i=0;;i++){
			if(*p=='-')
				break;
			temp[i]=*p;
			p++;
		}
		temp[i]='\0';
		p++;
		if(strcmp(name,temp)==0){
			unloadFile("/online","root");
			return 1;
		}
	}
	unloadFile("/online","root");
	return 0;
}


//登录的函数
void login(char *name)
{
	char psd[10];
	char tmp;
	int res;
	while(1)
	{
		printf("User Name:");
		cin>>name;
		printf("Password:");
      		for(int i=0; psd[i]=getch(); i++){
                    if(psd[i]=='\r'){
                        psd[i]='\0';
                        break;
                    }
                    cout<<"*";
                }
                
		res=verify(name,psd);
		//对验证用户后，返回不同值的不同处理
		if(res==1)
			printf("User %s is NOT exist!\n",name);
		else if(res==2)
			printf("Password is NOT correct!\n",name);
		else{
			//验证用户是否已登录
			if(haveLoged(name)==1){
				printf("The user is online ALREADY!\n");
			}
			else
				break; //成功则跳出循环
		}
		//判断是否继续输入用户名、密码
		while(1)
		{
			printf("[Y]Try again [N]Quit\n");
			cin>>tmp;
			if(tmp=='Y'||tmp=='y')
				break;
			else if(tmp=='N'||tmp=='n'){
				exit(0);
			}
			else
				printf("The value you entered is NOT valid, please input again!\n");
		}
	}

}



int addOnline(char *name)
{
	char stream[CONTENT_MAX];
	int openNum;
	short memoNum;
	openNum=loadFile("/online",RANDW,"online");
	if(openNum==FAIL){
		printf("System is busy... Please try again later.\n");
		unloadFile("/online",name);
		return FAIL;
	}

	openFileMemo[openNum].mod=1;
	memoNum=openFileMemo[openNum].sectMemo[0][1];
	strcpy(stream,strMemo[memoNum]);
	strcat(stream,name);
	strcat(stream,"-");
	strcpy(strMemo[memoNum],stream);

	unloadFile("/online",name);
	return SUCCESS;
}

int delOnline(char *name)
{
	char stream[CONTENT_MAX],temp[20];
	char *p,*q;
	int openNum,i;
	short memoNum;
	openNum=loadFile("/online",RANDW,"online");
	if(openNum==FAIL){
		printf("System is busy... Please try again later.\n");
		unloadFile("/online",name);
		return FAIL;
	}

	openFileMemo[openNum].mod=1;
	memoNum=openFileMemo[openNum].sectMemo[0][1];
	strcpy(stream,strMemo[memoNum]);
	
	p=stream;
	while(1){	
		q=p;
		for(i=0;;i++){
			if(*p=='-')
				break;
			temp[i]=*p;
			p++;
		}
		temp[i]='\0';
		p++;
		if(strcmp(name,temp)==0){
			while(1){
				*q=*p;
				if(*p=='\0'){
					strcpy(strMemo[memoNum],stream);
					unloadFile("/online",name);
					return SUCCESS;
				}
				q++;
				p++;
			}
		}
	}

}

int onlyRoot()
{
	char stream[CONTENT_MAX],temp[20];
	char *p,*q;
	int openNum,i;
	short memoNum;

	openNum=loadFile("/online",RANDW,"online");
	if(openNum==FAIL){
		printf("System is busy... Please try again later.\n");
		unloadFile("/online","root");
		return FAIL;
	}

	memoNum=openFileMemo[openNum].sectMemo[0][1];
	strcpy(stream,strMemo[memoNum]);

	p=stream;
	while(1){	
		q=p;
		if(*p=='\0')
			break;
		for(i=0;;i++){
			if(*p=='-')
				break;
			temp[i]=*p;
			p++;
		}
		temp[i]='\0';
		p++;
		if(strcmp("root",temp)!=0){
			printf("There are other users online! Please try agian later.\n");
			unloadFile("/online","root");
			return FAIL;
		}
	}
	unloadFile("/online","root");
	return SUCCESS;
}

int addUserHead(char *newName,long modTime){
	int i,n,sect,vNum;
	char name[20];
	HeadIndex hIdx[INDEX_MAX];
	
	n=strlen(newName);
	name[0]='/';
	for(i=0;i<n;i++){
		name[i+1]=newName[i];
	}
	name[i+1]='\0';
	
	vNum=loadFile("/vac",RANDW,"vac");
	if(vNum==FAIL){
		printf("load file /vac FAILED!\n");
		return FAIL;
	}
	sect=getVacSect(vNum);
	unloadFile("/vac","root");

	loadHeadIndex(hIdx);
	for(i=0;i<INDEX_MAX;i++){
		if(hIdx[i].type==TYPE_NONE){
			strcpy(hIdx[i].name,name);
			hIdx[i].PMS=NONE;
			hIdx[i].sects[0]=sect;
			hIdx[i].type=TYPE_DIR;
			hIdx[i].size=1;
			hIdx[i].modTime=modTime;
			hIdx[i].rdCnt=0;
			hIdx[i].wrtCnt=0;
			break;
		}
	}
	unloadHeadIndex(hIdx);
	return SUCCESS;
}



int delUserHead(char *name){
	int i,j,n,vNum,size;
	char tempName[50];
	HeadIndex headIndex[INDEX_MAX];
	
	//查找该文件
	n=strlen(name);

	loadHeadIndex(headIndex);
	for(i=0;i<INDEX_MAX;i++){
		if(headIndex[i].type!=TYPE_NONE){
			strcpy(tempName,headIndex[i].name);
			if(tempName[n+1]=='/'||tempName[n+1]=='\0'){
				tempName[n+1]='\0';
				if(strcmp(tempName+1,name)==0){
					strcpy(headIndex[i].name,"\0");
					headIndex[i].type=TYPE_NONE;

					//写回
					unloadHeadIndex(headIndex);
					
					//撤销磁盘空间
					size=headIndex[i].size;
					vNum=loadFile("/vac",RANDW,"vac");
					if(vNum==FAIL){
						printf("load file /vac FAILED!\n");
						return FAIL;
					}
					for(j=0;j<size;j++){
						delSect(vNum,headIndex[i].sects[j]);
					}
					unloadFile("/vac",name);	
				}
			}
		}
	}
	unloadHeadIndex(headIndex);
	return SUCCESS;
}
